from time import sleep
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

user_name = input('请输入用户名：')
password = input('请输入密码：')

# 去除浏览器识别（自动化检测软件字样）
option = webdriver.ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_experimental_option("detach", True)

web = webdriver.Chrome(options=option)

web.maximize_window()

web.get('https://kyfw.12306.cn/otn/resources/login.html')

sleep(3)

# 点击账号登录
web.find_element_by_xpath('//*[@id="toolbar_Div"]/div[2]/div[2]/ul/li[1]/a').click()

web.find_element_by_id('J-userName').send_keys(f'{user_name}')

web.find_element_by_id('J-password').send_keys(f'{password}')

sleep(1)

web.find_element_by_id('J-login').click()

sleep(1)

# 定位滑块 原位置
slider = web.find_element_by_xpath('/html/body/div[1]/div[4]/div[2]/div[2]/div/div/div[2]/div/div[1]/span')

# 获取滑块的宽度
slider_width = slider.size['width']

# 定位滑动条
slider_con = web.find_element_by_xpath('/html/body/div[1]/div[4]/div[2]/div[2]/div/div/div[2]/div/div[1]/div[2]/span')

# 获取滑动条宽度
slider_con_width = slider_con.size['width']

# 计算滑动宽度
offset = slider_con_width - slider_width

# 解决滑块失败问题
script = 'Object.defineProperty(navigator, "webdriver", {get: () => false,});'
web.execute_script(script)

# 滑动滑块
ActionChains(web).drag_and_drop_by_offset(slider,offset,slider_width).perform()

sleep(3)

# 点击进入页面之后的确定按钮
web.find_element_by_xpath('//*[@id="pop_16544405833272235"]/div[2]/div[3]/a').click()



web.quit()

